<?php if(!defined('PUBLIC_DIR')) die('No Access!');

Cnamespace('System');
class Action {
	private $ReflectionMethodInstance = null;
	private $ObjectInstance = null;
	private $Method = null;

	public function __construct($invokeInstruction){
		$invokeInstruction = str_replace('->', '::', $invokeInstruction);
		if(strpos($invokeInstruction, '.') !== false){
			$namespace = '';
			if(strpos($invokeInstruction, '::') === false){
				$invokeInstruction = substr_replace($invokeInstruction, '::', strrpos($invokeInstruction, '.'), 1);
			}
			$namespaceEndPosition = strrpos($invokeInstruction, '.');
			$namespace = substr($invokeInstruction, 0, $namespaceEndPosition);
			$invokeInstruction = ltrim(substr($invokeInstruction, $namespaceEndPosition), '.');
			if($namespace !== null && strlen($namespace) !== 0)
				using($namespace);
		}
		if(function_exists($invokeInstruction)){
			$this->Method = $invokeInstruction;
		}else if(is_callable($invokeInstruction)){
			$reflectionMethodInstance = new ReflectionMethod($invokeInstruction);
			$this->ReflectionMethodInstance = $reflectionMethodInstance;
			$Class = $reflectionMethodInstance->class;
			$this->Method = $reflectionMethodInstance->name;
			if($reflectionMethodInstance->isStatic() === True && $reflectionMethodInstance->isPublic() === True){
				$this->ObjectInstance = $Class;
			}else{
				$this->ObjectInstance = new $Class();
			}
		}else{
			trigger_error($invokeInstruction . ' is not callable!', E_USER_ERROR);
		}
	}
	
	public function hasReflectionMethodInstance() { return $this->ReflectionMethodInstance !== null; }
	public function getReflectionMethodInstance() { return $this->ReflectionMethodInstance; }
	public function hasObjectInstance() { return $this->ObjectInstance !== null; }
	public function getObjectInstance() { return $this->ObjectInstance; }
	public function hasMethod() { return $this->Method !== null; }
	public function getMethod() { return $this->Method; }
}
